package com.mskj.zxmy.controller;

import com.mskj.zxmy.common.Result;
import com.mskj.zxmy.dto.*;
import com.mskj.zxmy.entity.*;
import com.mskj.zxmy.service.OutboundOrderService;
import com.mskj.zxmy.service.StockInService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/api/outbound")
@Tag(name = "出库接口")
@CrossOrigin
public class OutboundController {

    @Autowired
    private OutboundOrderService outboundOrderService;
    @Autowired
    private StockInService stockInService;

    /**
     * 查询发货通知单列表（分页）
     *
     * @return
     */
    @PostMapping("/outboundOrderList")
    @Operation(summary = "发货通知单列表（分页）")
    public Result<PageInfo<ShipmentNoticeDTO>> outboundOrderList(@RequestBody OutboundOrderListQueryDTO query) {
        PageInfo<ShipmentNoticeDTO> result = outboundOrderService.outboundOrderListWithPage(query);
        for (ShipmentNoticeDTO shipmentNoticeDTO : result.getList()) {
            shipmentNoticeDTO.setDateTitle1(shipmentNoticeDTO.getClientId() + " " + shipmentNoticeDTO.getClientName());
        }
        return Result.success(result);
    }

    @GetMapping("/shipment/details")
    @Operation(summary = "根据发货通知单查询发货明细")
    public Result<List<XssalesDispatchDetails>> getShipmentDetails(@RequestParam String shipmentOrderNumber) {
        List<XssalesDispatchDetails> details = outboundOrderService.getDispatchDetailsByShipmentOrderNumber(shipmentOrderNumber);
        return Result.success(details);
    }

    @PostMapping("/shipmentNotice/filter")
    @Operation(summary = "发货通知单条件分页筛选")
    public Result<PageInfo<ShipmentNoticeDTO>> filterShipmentNotice(@RequestBody OutboundOrderListQueryDTO query) {
        PageInfo<ShipmentNoticeDTO> result = outboundOrderService.outboundOrderListWithPage(query);
        for (ShipmentNoticeDTO shipmentNoticeDTO : result.getList()) {
            shipmentNoticeDTO.setDateTitle1(shipmentNoticeDTO.getClientId() + " " + shipmentNoticeDTO.getClientName());
        }
        return Result.success(result);
    }

    /**
     * 生成出库单
     *
     * @return
     */
    @PostMapping("/createOutbound")
    @Operation(summary = "生成出库单")
    public Result<Map<String, CKDan>> createOutbound() {
        return Result.success(outboundOrderService.createOutbound());
    }

    /**
     * 根据产品入库批次号创建出库明细
     */
    @GetMapping("/getProductInfoByBatchNumber")
    @Operation(summary = "根据入库明细id创建出库明细")
    public Result<ProductDetailDTO> getProductInfoByDetailId(@RequestParam String detailId) {
        return Result.success(stockInService.getProductInfoByDetailId(detailId));
    }

    @PostMapping("/saveStockInWithDetails")
    @Operation(summary = "保存出库单及明细")
    public Result<CKDanSaveDTO> saveCKDanWithDetails(@RequestBody CKDanSaveDTO dto) {
        boolean success = outboundOrderService.saveCKDanWithDetails(dto);
        return success ? Result.success("保存成功") : Result.failed("保存失败");
    }


//    @PostMapping("/getCKDanList")
//    @Operation(summary = "查询出库单列表（分页）")
//    public Result<PageInfo<CKDanListDTO>> getCKDanList(@RequestBody CKDanListQueryDTO query) {
//        return Result.success(outboundOrderService.getCKDanListWithPage(query));
//    }


    @PostMapping("/filterCKDanList")
    @Operation(summary = "筛选出库单列表")
    public Result<List<CKDanListDTO>> filterCKDanList(@RequestBody Map<String, Object> params) {
        return Result.success(outboundOrderService.filterCKDanList(params));
    }


    @GetMapping("/getCKDanListDetails/{cashiersNumber}")
    @Operation(summary = "根据出库单查询出库单明细")
    public Result<CKDanSaveDTO> getCKDanListDetails(@PathVariable String cashiersNumber) {
        return Result.success(outboundOrderService.getCKDanListDetails(cashiersNumber));
    }

    @GetMapping("/getCKDanDetailsByXSDan")
    @Operation(summary = "根据销售发货单查询出库单")
    public Result<List<CKDanSaveDTO>> getCKDanDetailsByXSDan(@RequestParam String xsd) {
        List<CKDanSaveDTO> ckDanSaveDTOList = outboundOrderService.getCKDanDetailsByXSDan(xsd);
        return Result.success(ckDanSaveDTOList);
    }

    @PostMapping("/confirmOutbound")
    @Operation(summary = "确认出库")
    public Result<String> confirmOutbound(@RequestBody BarcodeManagement barcode) {
        try {
            outboundOrderService.confirmOutbound(barcode);
            return Result.success("出库成功");
        } catch (RuntimeException e) {
            return Result.failed(e.getMessage());
        }
    }

    @PostMapping("/getCKDanList")
    @Operation(summary = "查询出库单列表（分页）")
    public Result<PageInfo<CKDanListDTO>> getCKDanList(@RequestBody CKDanListQueryDTO query) {
        return Result.success(outboundOrderService.getCKDanListWithPage(query));
    }


    @GetMapping("/outboundDetail/{detailId}")
    @Operation(summary = "根据出库明细id查询出库明细")
    public Result<ck02CheckoutFormCheckout> getOutboundDetailById(@PathVariable("detailId") String detailId) {
        ck02CheckoutFormCheckout detail = outboundOrderService.getOutboundDetailById(detailId);
        return detail != null ? Result.success(detail) : Result.failed("未找到该出库明细");
    }

    @PostMapping("/outboundDetails/filter")
    @Operation(summary = "出库明细多条件筛选（分页）")
    public Result<PageInfo<ck02CheckoutFormCheckout>> filterOutboundDetails(@RequestBody OutboundDetailQueryDTO query) {
        PageInfo<ck02CheckoutFormCheckout> page = outboundOrderService.filterOutboundDetailsWithPage(query);
        return Result.success(page);
    }


}
